//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.model;

import com.aidong.model.wills.SysMenu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MenuAuthority extends SysMenu {
    protected int authority;
    protected Map<String, Integer> opexMap;
    protected static String[] opCode = new String[]{"list", "add", "edit", "del", "expt"};
    protected static String[] opName = new String[]{"查询", "新增", "修改", "删除", "导出"};

    public MenuAuthority() {
    }

    public MenuAuthority(Integer menuId) {
        this.menuId = menuId;
    }

    public int getAuthority() {
        return this.authority;
    }

    public void setAuthority(int authority) {
        this.authority = authority;
    }

    public void setOperateEx(String operateEx) {
        this.operateEx = operateEx;
        this.opexMap = new LinkedHashMap();
        if(this.operateEx != null && this.operateEx.length() > 0) {
            int n = 256;
            String[] arr$ = this.operateEx.split(",");
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String cc = arr$[i$];
                String[] c = cc.split("\\|");
                this.opexMap.put(c[0], Integer.valueOf(n));
                n *= 2;
            }
        }

    }

    public boolean chkAuth(String op) {
        if(op != null && op.length() != 0) {
            op = op.toLowerCase();
            int opId = this.getOpId(op);
            if(opId == 0 && op.startsWith("_")) {
                opId = this.getOpId(op.substring(1));
            }

            if(opId == 0 && op.startsWith("do")) {
                opId = this.getOpId(op.substring(2));
            }

            return this.chkAuth(opId);
        } else {
            return false;
        }
    }

    public boolean chkAuth(int opId) {
        return (this.authority & opId) > 0;
    }

    public int getOpId(String op) {
        int opId = 0;
        if(!"query".equals(op) && !"list".equals(op) && !"view".equals(op) && !"info".equals(op) && !"detail".equals(op) && !"get".equals(op) && !op.startsWith("get") && !op.startsWith("view")) {
            if("add".equals(op)) {
                opId = 2;
            } else if("edit".equals(op)) {
                opId = 4;
            } else if("del".equals(op)) {
                opId = 8;
            } else if("expt".equals(op)) {
                opId = 16;
            } else if(this.opexMap != null) {
                Integer n = (Integer)this.opexMap.get(op);
                if(n != null) {
                    opId = n.intValue();
                }
            }
        } else {
            opId = 1;
        }

        return opId;
    }

    public List<String> getOperList() {
        ArrayList list = new ArrayList();
        int n = 2;

        for(int i = 1; i < opCode.length; ++i) {
            if((this.authority & n) > 0) {
                list.add(opCode[i]);
            }

            n *= 2;
        }

        if(this.opexMap != null) {
            Iterator i$ = this.opexMap.keySet().iterator();

            while(i$.hasNext()) {
                String opx = (String)i$.next();
                if((this.authority & ((Integer)this.opexMap.get(opx)).intValue()) > 0) {
                    list.add(opx);
                }
            }
        }

        return list;
    }

    public String getAuthHtml() {
        int n = 1;
        int p = (this.operate == null?0:this.operate.intValue()) / 2;
        int pm = this.authority / 2;
        StringBuffer sb = new StringBuffer();

        while(p >= 1) {
            if(p % 2 == 1) {
                sb.append("<input type=\'checkbox\' name=\'auth_" + this.menuId + "\' id=\'auth_" + this.menuId + "_" + n + "\' value=" + (int)Math.pow(2.0D, (double)n));
                if(pm % 2 == 1) {
                    sb.append(" checked");
                }

                sb.append(" onclick=\'checkFirst(this);\'><label for=\'auth_" + this.menuId + "_" + n + "\'>" + opName[n] + "</label>");
            }

            p /= 2;
            pm /= 2;
            ++n;
            if(n >= opCode.length) {
                break;
            }
        }

        if(this.operateEx != null && this.operateEx.length() > 0) {
            n = 256;
            String[] arr$ = this.operateEx.split(",");
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String cc = arr$[i$];
                String[] c = cc.split("\\|");
                sb.append("<input type=\'checkbox\' name=\'auth_" + this.menuId + "\' id=\'auth_" + this.menuId + "_" + n + "\' value=" + n);
                if((this.authority & n) > 0) {
                    sb.append(" checked");
                }

                sb.append(" onclick=\'checkFirst(this);\'><label for=\'auth_" + this.menuId + "_" + n + "\'>" + c[1] + "</label>");
                n *= 2;
            }
        }

        return sb.toString();
    }

    public String getOperateHtml() {
        int p = (this.operate == null?0:this.operate.intValue()) / 2;
        StringBuffer sb = new StringBuffer();

        for(int i = 1; i < opName.length; ++i) {
            sb.append("<input type=\'checkbox\' name=\'operates\' id=\'operate_" + i + "\' value=" + (int)Math.pow(2.0D, (double)i));
            if(p % 2 == 1) {
                sb.append(" checked");
            }

            sb.append("><label for=\'operate_" + i + "\'>" + opName[i] + "</label>");
            p /= 2;
        }

        return sb.toString();
    }
}
